<?php

function getUserPreferences() {

	session_start();

	include('../../sidebardbinfo-live.php');

	// connect to the database
	$prefslink = mysql_connect($sidebardbhost, $sidebardbuser, $sidebardbpass);
	if (!$prefslink) {
		die('Problem connecting to database server : ' . mysql_error());
	}

	// make $dbname the current db
	$prefsdb_selected = mysql_select_db($sidebarprefsdbname, $prefslink);
	if (!$prefsdb_selected) {
		die ('Problem connecting to database : ' . mysql_error());
	}

	$prefsstmt = "SELECT lan, sheight, squantity, player, qikstatus from prefs WHERE username = '".$_SESSION['seesmicName']."'";

	$prefsres = mysql_query($prefsstmt,$prefslink);
	if (!$prefsres) {
		die ('Problem with query : ' . mysql_error());
	}

	$prefsrow = mysql_fetch_row($prefsres);

	if ($prefsrow) {
		// preferences found for user
		$_SESSION['lan'] = $prefsrow[0];
		$_SESSION['sheight'] = $prefsrow[1];
		$_SESSION['squantity'] = $prefsrow[2];
		$_SESSION['player'] = $prefsrow[3];
		$_SESSION['qikStatus'] = $prefsrow[4];
	} else {
		// no preferences found, set defaults and update prefs database
		$_SESSION['lan'] = "all";
		$_SESSION['sheight'] = 360;
		$_SESSION['squantity'] = 20;
		$_SESSION['player'] = "small";
		$_SESSION['qikStatus'] = "on";
		$prefsinsertstmt = "INSERT INTO prefs (username, lan, sheight, squantity, player, qikstatus) VALUES ('".$_SESSION['seesmicName']."','all',360,20,'small','on')";

		$prefsinsertres = mysql_query($prefsinsertstmt,$prefslink);
		if (!$prefsinsertres) {
			die ('Problem with query : ' . mysql_error());
		}
	}

	// close the database link
	mysql_close($prefslink);
}

function updateUserPreferences() {

	session_start();

	include('../../sidebardbinfo-live.php');

	// connect to the database
	$updateprefslink = mysql_connect($sidebardbhost, $sidebardbuser, $sidebardbpass);
	if (!$updateprefslink) {
		die('Problem connecting to database server : ' . mysql_error());
	}

	// make $dbname the current db
	$updateprefsdb_selected = mysql_select_db($sidebarprefsdbname, $updateprefslink);
	if (!$updateprefsdb_selected) {
		die ('Problem connecting to database : ' . mysql_error());
	}

	$updateprefsstmt = "UPDATE prefs SET lan = '".$_SESSION['lan']."', sheight = ".$_SESSION['sheight'].", squantity = ".$_SESSION['squantity'].", player = '".$_SESSION['player']."', qikstatus = '".$_SESSION['qikStatus']."' WHERE username = '".$_SESSION['seesmicName']."'";

	$updateprefsres = mysql_query($updateprefsstmt,$updateprefslink);
	if (!$updateprefsres) {
		die ('Problem with query : ' . mysql_error());
	}

	// close the database link
	mysql_close($updateprefslink);
}
?>